package com.xmb.wechat.delegate;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.ImageView;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.nil.sdk.utils.DateUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.baserecyclerview.BaseRecyclerViewManager;
import com.xmb.wechat.bean.TradeHistoryBean;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.util.MoneyFormat;
import com.xmb.wechat.util.PicLoadUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.Date;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/20 17:54
 */
public class TradeHistoryDelegate extends BaseRecyclerViewManager {

    public TradeHistoryDelegate(Activity activity) {
        super(activity);
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        return new BaseQuickAdapter<TradeHistoryBean, BaseViewHolder>(R.layout.item_trade_history, new ArrayList<>()) {
            @SuppressLint("SimpleDateFormat")
            @Override
            protected void convert(BaseViewHolder helper, TradeHistoryBean item) {
                ImageView imageView = helper.getView(R.id.iv_icon);
                switch (item.getType()) {
                    case Constant.TradeHistoryType.TYPE_ZIDINGYI://自定义
                    case Constant.TradeHistoryType.TYPE_ZHUANZHANG://转账
                        PicLoadUtils.setupRoundPicIntoImageView(item.getIconSourceType(), item.getIconUrl(), imageView);
                        //money
                        showMoneyWithDifferentColor(helper,item);
                        break;
                    case Constant.TradeHistoryType.TYPE_RED_PACKET://红包
                        if (item.isExpend()) {
                            PicLoadUtils.setupRoundPicIntoImageView(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE,
                                    String.valueOf(R.drawable.icon_trade_history_hb), imageView);
                        } else {
                            PicLoadUtils.setupRoundPicIntoImageView(item.getIconSourceType(), item.getIconUrl(), imageView);
                        }
                        //money
                        showMoneyWithDifferentColor(helper,item);
                        break;
                    case Constant.TradeHistoryType.TYPE_QRCODE://二维码
                        //avatar
                        if (item.isExpend()) {
                            PicLoadUtils.setupRoundPicIntoImageView(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE,
                                    String.valueOf(R.drawable.icon_trade_history_ewm), imageView);
                        } else {
                            PicLoadUtils.setupRoundPicIntoImageView(item.getIconSourceType(), item.getIconUrl(), imageView);
                        }
                        //money
                        showMoneyWithDifferentColor(helper,item);
                        break;
                    case Constant.TradeHistoryType.TYPE_CHANGE://零钱
                        PicLoadUtils.setupRoundPicIntoImageView(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE,
                                String.valueOf(R.drawable.icon_trade_history_lq), imageView);
                        //money
                        showMoneyWithSameColor(helper,item);
                        break;
                }

                helper.setText(R.id.tv_type, TextUtils.isEmpty(item.getContent()) ? "" : item.getContent());

                helper.setText(R.id.tv_time, DateUtils.getStringByFormat(new Date(item.getTradeTime()), "yyyy-MM-dd HH:mm"));

                helper.getView(R.id.iv_delete).setOnClickListener(v -> {
                    WechatStartApplication.getBoxStore().boxFor(TradeHistoryBean.class).remove(item);
                    mData.remove(item);
                    notifyDataSetChanged();
                });
            }
        };
    }

    /**
     * 收入与支出不一样颜色
     * @param helper
     * @param item
     */
    private void showMoneyWithDifferentColor(BaseViewHolder helper, TradeHistoryBean item) {
        if (item.isExpend()) {
            helper.setTextColor(R.id.tv_money, context.getResources().getColor(R.color.yellow_70));
            helper.setText(R.id.tv_money, "-" + MoneyFormat.formatMoney(item.getMoney()));
        } else {
            helper.setTextColor(R.id.tv_money, context.getResources().getColor(R.color.black));
            helper.setText(R.id.tv_money, "+" + MoneyFormat.formatMoney(item.getMoney()));
        }
    }

    /**
     * 收入与支出颜色一样
     * @param helper
     * @param item
     */
    private void showMoneyWithSameColor(BaseViewHolder helper, TradeHistoryBean item) {
        helper.setTextColor(R.id.tv_money, context.getResources().getColor(R.color.black));
        helper.setText(R.id.tv_money,MoneyFormat.formatMoney(item.getMoney()));
    }

    @Override
    protected RecyclerView.ItemDecoration getItenDecoration() {
        return new HorizontalDividerItemDecoration.Builder(context).size(SizeUtils.dp2px(0.5f)).color(ContextCompat.getColor(context, R.color.black_10)).build();
    }
}
